//
//  HPLoginViewController.m
//  HPZZTong
//
//  Created by handpay on 14-3-21.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPLoginViewController.h"
#import "Utility.h"
#import <HandPaySwiperModule/HandPaySwiperModule.h>

@interface HPLoginViewController ()

@end

@implementation HPLoginViewController

#if ZZT_FOR_TEST
#define PasreKey(key)   [respData objectForKey:key]
#else
#define PasreKey(key)   DES_DEC([respData objectForKey:key]);
#endif

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.showBackButton = NO;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    NSString* userName = [HPCLIENTENG GetRMSString:KEY_USER_NAME];
    if (![NSString isNilOrEmpty:userName]) {
        self.userNameTextField.text = userName;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    [self.userNameTextField setConditionType:HPConditionType_Alphanumeric];
    [self.userNameTextField setMaxLength:20];
    
    if (!HP_Device_iPhone5) {
        self.bgImageView.image = [UIImage imageNamed:@"loginbg_4.png"];
        
        CGRect tempFrame = self.bgImageView.frame;
        tempFrame.size.height = 233;
        self.bgImageView.frame = tempFrame;
        
        CGRect headFrame = self.headImageView.frame;
        headFrame.origin.y -= 40;
        self.headImageView.frame = headFrame;
    }
    
    if (!iOS7Later) {
        CGRect tempFrame = self.bgImageView.frame;
        tempFrame.origin.y -= HEIGHT_STATUS_BAR;
        self.bgImageView.frame = tempFrame;
    }
    
    CGRect frame = self.headImageView.frame;
    
    frame.size.width = self.headImageView.image.size.width / 2;
    frame.size.height = self.headImageView.image.size.height / 2;
    
    frame.origin.x = (self.bgImageView.frame.size.width - frame.size.width) / 2;
    frame.origin.y = (self.bgImageView.frame.size.height - frame.size.height) / 2;
    self.headImageView.frame = frame;
    
    [self.loginButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.loginButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
    [self.registerButton setBackgroundImage:[[UIImage imageNamed:@"whitebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.registerButton setBackgroundImage:[[UIImage imageNamed:@"whitebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
    self.inputBgImageView.image = [[UIImage imageNamed:@"logininputbg.png"] stretchableImageWithLeftCapWidth:8 topCapHeight:0];
    
    [self setLeftView:self.userNameTextField imgName:@"loginuserhover.png"];
    [self setLeftView:self.pwdTextField imgName:@"loginpassword.png"];
    self.pwdTextField.secureTextEntry = YES;
    [self.pwdTextField setMaxLength:20];
}

- (void)setLeftView:(HPConditionTextField*)textField imgName:(NSString*)imgName{
    UIImage* img = [UIImage imageNamed:imgName];
    UIImageView* leftImageView = [[UIImageView alloc] initWithImage:img];
    CGSize size = [img realSize];
    leftImageView.frame = CGRectMake(0, 0, size.width, size.height);
    textField.leftView = leftImageView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.edgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [leftImageView release];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
//    [SwiperService checkAndConnectSwiper:nil];
}
- (void)dealloc {
    [_loginButton release];
    [_registerButton release];
    [_userNameTextField release];
    [_pwdTextField release];
    [_bgImageView release];
    [_headImageView release];
    [_inputBgImageView release];
    [_forgetPwdButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setLoginButton:nil];
    [self setRegisterButton:nil];
    [self setUserNameTextField:nil];
    [self setPwdTextField:nil];
    [self setBgImageView:nil];
    [self setHeadImageView:nil];
    [self setInputBgImageView:nil];
    [self setForgetPwdButton:nil];
    [super viewDidUnload];
}

- (IBAction)loginClick:(id)sender {
    //校验
    NSString* userName = self.userNameTextField.text;
    if ([NSString isNilOrEmpty:userName]) {
        [HPUIENG showAlert:@"请输入账户或手机号"];
        [self.userNameTextField becomeFirstResponder];
        return;
    }
    
    NSString* pwd = self.pwdTextField.text;
    
    if ([NSString isNilOrEmpty:pwd]) {
        [HPUIENG showAlert:@"请输入密码"];
        [self.pwdTextField becomeFirstResponder];
        return;
    }
    
    self.pwdTextField.text = @"";
    
    [self.pwdTextField resignFirstResponder];
    [self.userNameTextField resignFirstResponder];
    
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setObject:DES_ENC(userName) forKey:KEY_ACCOUNT];
    [params setObject:DES_ENC(pwd) forKey:@"pwd"];
    [params setObject:[zzt getChannel] forKey:KEY_CHANNEL];
#if ZZT_FOR_TEST
    
    [self responseCallBackSuccess:NET_ACTION_ZZT_LOGIN withData:@{KEY_ACCOUNT:@"xuyue", @"status":@"00",
                                                                  KEY_PHONE:@"13813713613",
                                                                  @"shopName":@"朱阿斯达"
                                                                  }];
#else
    
    [zzt.netEngine sendRequest:NET_ACTION_ZZT_LOGIN withParams:params withDelegate:self];
    self.pwdTextField.text = @"";
    [zzt.uiEngine showNetWaiting];
#endif
}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    [HPUIENG removeWaiting];
   
    if ([action isEqualToString:NET_ACTION_ZZT_LOGIN]) {
        ZZT_CLIENG.accountInfo.account = PasreKey(KEY_ACCOUNT);
        ZZT_CLIENG.accountInfo.mobile = PasreKey(KEY_PHONE);
//        [SwiperService setCsnStatus:CSNStatusNone];// mcm
        [HPSwiperContext sharedInstance].avaliableService.csnStatus = CSNStatusNone; // mcm new
        NSString* status = [respData objectForKey:@"status"];
        [ZZT_CLIENG.accountInfo setstatusByStr:status];
//        ZZT_CLIENG.accountInfo.accountStatus = AccountStatusNotSubmit;

        
        ZZT_CLIENG.accountInfo.shopName = PasreKey(@"shopName");

        ZZT_CLIENG.accountInfo.statusDescpriction =  [respData objectForKey:@"failDescpriction"];
		[HPCLIENTENG SaveRMSData:KEY_USER_NAME withStr:self.userNameTextField.text];

        [HPUIENG showIndex:1];
    }
}

- (IBAction)registerClick:(id)sender
{
//    if ([ZZT_CLIENG getVerifyVersionStatus] == VerifyVerionStatus_Failed)
//    {
//        [HPUIENG showAlert:@"版本检测不通过"];
//        [ZZT_CLIENG doVerifyVersion];
//        return;
//    }
    if (HPCLIENTENG.netStatus == 0) {
        [HPUIENG showAlert:@"抱歉，连接网络不成功，建议您检查手机网络和连接设置后重新再试。(0001)"];
        return;
    }
    [HPUIENG showScreen:SCR_REGISTER];
}

- (IBAction)forgetPwdClick:(id)sender {
    [self.pwdTextField resignFirstResponder];
    [self.userNameTextField resignFirstResponder];
    [HPUIENG showScreen:SCR_FORGET_PWD];
}
@end
